import javax.swing.*;

import java.awt.*;
import java.util.Observable;
import java.util.Observer;

public class GameTable extends JPanel implements Runnable, Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public JSplitPane split_pane;
	public GameFlowDisplay game_flow_display;
	public GameFlowControl game_flow_control_panel;
	public GameTablePlayerReadyButtonObservable game_player_ready_button_observable;
	public GameTableTurnReadyButtonObservable game_turn_ready_button_observable;
	public GameFrameRepaintObservable game_frame_repaint_from_game_table;

	public GameTable(GameFlowDisplay game_display, GameFlowControl game_control) {

		this.setLayout(new BorderLayout());
		this.game_flow_display = game_display;
		this.game_flow_control_panel = game_control;

		this.game_flow_control_panel.place_plane_button_observable
				.addObserver(this);
		this.game_flow_control_panel.undo_plane_button_observable
				.addObserver(this);
		this.game_flow_control_panel.player_ready_button_observable
				.addObserver(this);
		this.game_flow_control_panel.turn_ready_button_observable
				.addObserver(this);
		this.game_flow_control_panel.plane_direction_up_observable
				.addObserver(this);
		this.game_flow_control_panel.plane_direction_right_observable
				.addObserver(this);
		this.game_flow_control_panel.plane_direction_down_observable
				.addObserver(this);
		this.game_flow_control_panel.plane_direction_left_observable
				.addObserver(this);

		this.game_player_ready_button_observable = new GameTablePlayerReadyButtonObservable();
		this.game_turn_ready_button_observable = new GameTableTurnReadyButtonObservable();
		this.game_frame_repaint_from_game_table = new GameFrameRepaintObservable();

		this.addMouseListener(this.game_flow_display);

		JSplitPane split_pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		split_pane.setResizeWeight(0.8);
		split_pane.setDividerLocation(0.8);
		split_pane.setRightComponent(game_control);
		split_pane.setLeftComponent(game_display);
		this.add(split_pane, BorderLayout.CENTER);
	}

	public void run() {
		while (true) {

			if (this.game_flow_display.to_repaint == true) {
				this.repaint();
				this.game_flow_display.to_repaint = false;
			}
			
			if (this.game_flow_display.plane_undo_to_be_performed == true)
			{
				this.game_flow_control_panel.undo_plane_to_be_performed = true;
				this.game_flow_display.plane_undo_to_be_performed = false;
			}

			if (this.game_flow_control_panel.place_planes_button.isVisible() == false) {
				this.game_flow_display.plane_positioning_phase = false;
			} else {
				this.game_flow_display.plane_positioning_phase = true;
			}

			if (this.game_flow_control_panel.player_ready_button_pressed == true) {
				this.game_flow_control_panel.player_ready_button_pressed = false;
			}
			if (this.game_flow_display.plane_set_point_selected == true){
				this.game_flow_display.plane_set_point_selected = false;
				this.game_flow_control_panel.plane_set_point_selected = true;
			}
		}
	}

	public void update(Observable o, Object arg) {

		if (o.equals(this.game_flow_control_panel.place_plane_button_observable)) {
			this.game_flow_display.plane_button_pressed = true;
		}
		if (o.equals(this.game_flow_control_panel.plane_direction_down_observable)) {
			this.game_flow_display.plane_direction = 2;
		}
		if (o.equals(this.game_flow_control_panel.plane_direction_up_observable)) {
			this.game_flow_display.plane_direction = 0;
		}
		if (o.equals(this.game_flow_control_panel.plane_direction_left_observable)) {
			this.game_flow_display.plane_direction = 3;
		}
		if (o.equals(this.game_flow_control_panel.plane_direction_right_observable)) {
			this.game_flow_display.plane_direction = 1;
		}
		if (o.equals(this.game_flow_control_panel.undo_plane_button_observable)) {
			this.game_flow_display.plane_undo_button_pressed = true;
		}
		if (o.equals(this.game_flow_control_panel.player_ready_button_observable)) {
			this.game_player_ready_button_observable.buttonPress();
		}
		if (o.equals(this.game_flow_control_panel.turn_ready_button_observable)) {
			this.game_turn_ready_button_observable.buttonPress();
		}

	}

}

class GameTablePlayerReadyButtonObservable extends Observable {
	public void buttonPress() {
		this.setChanged();
		this.notifyObservers();
	}

}

class GameFrameRepaintObservable extends Observable {
	public void RepaintObservable() {
		this.setChanged();
		this.notifyObservers();
	}

}

class GameTableTurnReadyButtonObservable extends Observable {
	public void buttonPress() {
		this.setChanged();
		this.notifyObservers();
	}

}
